﻿Public Class ctrDatBan

    Private Sub ctrDatBan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gvBan_Load()
        gvBanDat_Load()
        gvPhieuDat_Load()
    End Sub

    Private Sub gvBan_Load()
        Dim bus As BanBUS = New BanBUS()
        gvBan.DataSource = bus.GetTable()
    End Sub

    Private Sub gvPhieuDat_Load()
        Dim bus As PhieuDatBUS = New PhieuDatBUS()
        gvPhieuDat.DataSource = bus.GetTable()
    End Sub

    Private Sub gvBanDat_Load()
        Dim bus As BanDatBUS = New BanDatBUS()
        gvBanDat.DataSource = bus.GetTable()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim tmp As PhieuDatDTO = New PhieuDatDTO()
        tmp.TenKhach = txtTenKhach.Text
        tmp.DienThoai = txtDienThoai.Text
        tmp.SoNguoi = Integer.Parse(txtSoNguoi.Text)
        tmp.ThoiGian = dtThoiGian.Value

        Dim bus As PhieuDatBUS = New PhieuDatBUS()
        bus.Insert(tmp)

        gvBan_Load()
        gvBanDat_Load()
        gvPhieuDat_Load()
    End Sub

    Private Sub btnLamMoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLamMoi.Click
        txtTenKhach.Text = ""
        txtDienThoai.Text = ""
        txtSoNguoi.Text = ""
        dtThoiGian.Value = Now
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim tmp As PhieuDatDTO = New PhieuDatDTO()
        tmp.TenKhach = txtTenKhach.Text
        tmp.DienThoai = txtDienThoai.Text
        tmp.SoNguoi = Integer.Parse(txtSoNguoi.Text)
        tmp.ThoiGian = dtThoiGian.Value
        tmp.MaPD = gvPhieuDat.CurrentRow.Cells(0).Value.ToString()

        Dim bus As PhieuDatBUS = New PhieuDatBUS()
        bus.Update(tmp)

        gvBan_Load()
        gvBanDat_Load()
        gvPhieuDat_Load()
    End Sub

    Private Sub btnXoaPD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaPD.Click
        Dim tmp As PhieuDatDTO = New PhieuDatDTO()
        tmp.MaPD = gvPhieuDat.CurrentRow.Cells(0).Value.ToString()

        Dim bus As PhieuDatBUS = New PhieuDatBUS()
        bus.Delete(tmp)

        gvBan_Load()
        gvBanDat_Load()
        gvPhieuDat_Load()
    End Sub

    Private Sub btnXoaBD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaBD.Click
        Dim tmp As BanDatDTO = New BanDatDTO()
        tmp.MaPD = gvBanDat.CurrentRow.Cells(0).Value.ToString()
        tmp.MaBan = gvBanDat.CurrentRow.Cells(1).Value.ToString()

        Dim bus As BanDatBUS = New BanDatBUS()
        bus.Delete(tmp)

        Dim tmp2 As BanDTO = New BanDTO()
        tmp2.MaBan = gvBanDat.CurrentRow.Cells(1).Value.ToString()
        tmp2.TinhTrang = "Trống"

        Dim bus2 As BanBUS = New BanBUS()
        bus2.Update(tmp2)

        gvBan_Load()
        gvBanDat_Load()
        gvPhieuDat_Load()
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        frmMain.TabControl1.TabPages.Remove(frmMain.TabControl1.SelectedTab)
    End Sub

    Private Sub gvPhieuDat_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvPhieuDat.SelectionChanged
        If gvPhieuDat.CurrentRow IsNot Nothing Then
            txtTenKhach.Text = gvPhieuDat.CurrentRow.Cells(1).Value.ToString()
            txtDienThoai.Text = gvPhieuDat.CurrentRow.Cells(2).Value.ToString()
            txtSoNguoi.Text = gvPhieuDat.CurrentRow.Cells(3).Value.ToString()
            dtThoiGian.Text = gvPhieuDat.CurrentRow.Cells(4).Value.ToString()
        End If
    End Sub

    Private Sub gvBan_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gvBan.DoubleClick
        Dim tmp As BanDatDTO = New BanDatDTO()
        tmp.MaPD = gvPhieuDat.CurrentRow.Cells(0).Value.ToString()
        tmp.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()

        Dim bus As BanDatBUS = New BanDatBUS()
        bus.Insert(tmp)

        Dim tmp2 As BanDTO = New BanDTO()
        tmp2.MaBan = gvBan.CurrentRow.Cells(0).Value.ToString()
        tmp2.TinhTrang = "Đặt"

        Dim bus2 As BanBUS = New BanBUS()
        bus2.Update(tmp2)

        gvBan_Load()
        gvBanDat_Load()
        gvPhieuDat_Load()
    End Sub
End Class
